<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="searchForm" ref="searchForm">
            <el-form-item prop="modelName">
              <el-input v-model="searchForm.modelName" clearable placeholder="模板名称"></el-input>
            </el-form-item>
            <el-form-item prop="mainFlag">
              <el-select v-model="searchForm.mainFlag" :clearable="true" placeholder="是否默认模板">
                <el-option v-for="item in mainFlagList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="areaIds">
              <Region :areaValues="searchForm.areaIds" :isChangeOnSelect="true" @regionChange="handleRegionForSearch"></Region>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="onSearch">搜索</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="init" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
          <!--<span class="line">|</span>-->
          <!--<el-button type="primary" icon="fa fa-mail-forward">导出</el-button>-->
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="tableList" @row-dblclick="handleClickDetail" :fit="true">
        <el-table-column prop="modelId" label="模板编号"></el-table-column>
        <el-table-column prop="modelName" label="模板名称"></el-table-column>
        <el-table-column label="省-市-区">
          <template slot-scope="scope">
            <span>{{ scope.row.provinceName }} - {{ scope.row.cityName }} - {{ scope.row.areaName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="mainFlag" label="是否默认" width="120" :formatter="formatMainFlag"></el-table-column>
        <el-table-column prop="createUserName" label="创建人"></el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
              <el-dropdown trigger="click" size="small">
                <el-button type="primary"><i class="fa fa-list-ul"></i>更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                  <el-dropdown-item v-has="permissionCopy" @click.native="handleClickCopy(scope.row)">复制</el-dropdown-item>
                  <!--<el-dropdown-item v-has="permissionUpStatus" @click.native="handleClickDelete(scope.row, 0)">{{scope.row.state == 0? '启用' : '禁用'}}</el-dropdown-item>
                  <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state == 0" @click.native="handleClickDelete(scope.row, -1)">删除</el-dropdown-item>-->
                </el-dropdown-menu>
              </el-dropdown>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <el-dialog title="复制模板" :visible.sync="hideDialogShow" :center="isCenter" width="600px">
      <el-form :model="dataForm" ref="dataForm" :rules="roleRules" label-width="120px">
        <el-form-item label="是否默认模板" prop="mainFlag">
          <el-radio-group v-model="dataForm.mainFlag">
            <el-radio :label="1">默认</el-radio>
            <el-radio :label="2">非默认</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="模板名称" prop="modelName">
          <el-input v-model="dataForm.modelName"></el-input>
        </el-form-item>
        <el-form-item label="所在地区" prop="areaIds">
          <Region :areaValues="dataForm.areaIds" :isChangeOnSelect="true" @regionChange="handleRegionChange"></Region>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="dataForm.remark"></el-input>
        </el-form-item>
        <el-form-item align="center">
          <el-button type="primary" plain @click.native="clickCommit">确 定</el-button>
          <el-button @click.native="clickCancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import Region from '@/components/Region';
import NuclePriceApi from '@/api/sample/nuclePrice';
import * as message from '@/constants/message';

export default {
  name: 'NuclePriceModelManagement', // 缓存当前页面，需要给当前页面命名，并且在路由meta加上cache:true
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      tableList: [],
      dataForm: {
        modelId: '',
        areaIds: [],
        mainFlag: '',
        provinceId: '',
        cityId: '',
        areaId: '',
        provinceName: '',
        cityName: '',
        areaName: '',
      },
      collagePayerList: [],
      hospitalNameList: [],
      hideHospitalForm: {
        institutionId: '',
        hospitalSelIds: [],
        institutionSelName: '',
      },
      hideDialogShow: false,
      isCenter: true,

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },
      searchForm: {
        provinceId: '',
        cityId: '',
        areaId: '',
        areaIds: [],
        modelName: '',
        mainFlag: '',
      },
      mainFlagList: [
        { label: '是', value: '1' },
        { label: '否', value: '0' },
      ],
      roleRules: {
        modelName: [
          { required: true, message: '请输入模板名称', trigger: 'blur' },
          { max: 50, message: '模板名称长度不超过50个字', trigger: 'blur' },
        ],
        mainFlag: [{ required: true, message: '请选择是否默认模板', trigger: 'change' }],
        areaIds: [{ required: true, message: '请选择所在地区', trigger: 'change' }],
      },
      permissionCheck: 'SYSTEM_MANAGER_SAMPLE_NUCLE_PRICE_LIST_SEE',
      permissionAdd: 'SYSTEM_MANAGER_SAMPLE_NUCLE_PRICE_LIST_ADD',
      permissionEdit: 'SYSTEM_MANAGER_SAMPLE_NUCLE_PRICE_LIST_EDIT',
      permissionCopy: 'SYSTEM_MANAGER_SAMPLE_NUCLE_PRICE_LIST_COPY',
      permissionUpStatus: 'SYSTEM_MANAGER_SAMPLE_NUCLE_PRICE_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_SAMPLE_NUCLE_PRICE_LIST_DELETE',
    };
  },
  components: {
    Region,
  },
  created() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  //activated,deactivated这两个生命周期函数一定是要在使用了keep-alive组件后才会有的，否则则不存在
  activated() {
    this.init();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listTable();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    //拼团状态：(1.拼团中 2.已成团 3.已完成 4.已取消)
    formatMainFlag(row, column, cellValue) {
      if (cellValue == 1) {
        return '是';
      } else {
        return '否';
      }
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    getHospitalId(val) {
      this.searchForm.hospitalId = val;
    },
    clickCommit() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {}).then(() => {
            NuclePriceApi.copyData(this.dataForm).then((data) => {
              this.$Utils.notifySuccess();
              this.hideDialogShow = false;
              this.listTable();
            });
          });
        }
      });
    },
    clickCancel() {
      this.hideDialogShow = false;
      this.$refs.dataForm.resetFields();
      this.dataForm.areaIds = [];
    },
    handleClickCopy(row) {
      this.hideDialogShow = true;
      this.dataForm.modelId = row.modelId;
      this.dataForm.areaIds = [];
    },
    handleRegionChange(val) {
      this.dataForm.provinceId = val.provinceId;
      this.dataForm.provinceName = val.provinceName;
      this.dataForm.cityId = val.cityId;
      this.dataForm.cityName = val.cityName;
      this.dataForm.areaId = val.areaId;
      this.dataForm.areaName = val.areaName;
      this.dataForm.areaIds = [val.provinceId, val.cityId, val.areaId];
    },
    handleRegionForSearch(val) {
      this.searchForm.provinceId = val.provinceId;
      this.searchForm.cityId = val.cityId;
      this.searchForm.areaId = val.areaId;
      this.searchForm.areaIds = [val.provinceId, val.cityId, val.areaId];
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    handleClickAdd() {
      this.$router.push('/sample/nuclePriceModelAdd');
    },
    handleClickEditor(row) {
      this.$router.push({ path: '/sample/nuclePriceModelEdit', query: { id: row.modelId } });
    },

    /*handleClickDelete(row, state) {
      let msg = message.MSG_CONFIRM_DELETE
      if (state == 0){
        state = (row.state == 0)? 1 : 0;
        msg = (row.state == 0)? message.MSG_CONFIRM_ENABLE : message.MSG_CONFIRM_DISABLE;
      }
      this.$confirm(msg, message.TITLE_PROMPT, {})
      .then(() => {
        let data = {
          applyId: row.collageId,
          state: state
        };
        CollageApi.deleteData(data)
        .then(res => {
          this.listTable();
          this.$Utils.notifySuccess()
        })
      })
    },*/
    handleClickDetail(row) {
      this.$router.push({ path: '/sample/nuclePriceModelDetail', query: { id: row.modelId } });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.searchForm.provinceId = '';
      this.searchForm.cityId = '';
      this.searchForm.areaId = '';
      this.onSearch();
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        provinceId: this.searchForm.provinceId,
        cityId: this.searchForm.cityId,
        areaId: this.searchForm.areaId,
        modelName: this.searchForm.modelName,
        mainFlag: this.searchForm.mainFlag,
      };
      NuclePriceApi.pageData(data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
